package com.pro.xwt.rest.controller;

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pro.xwt.common.model.BEmployee;
import com.pro.xwt.common.model.BTask;
import com.pro.xwt.common.service.BTaskService;
import com.pro.xwt.common.utils.Common;
import com.pro.xwt.rest.ResponseResult;

@Controller
@RequestMapping("/tasks")
public class BTaskController {
	@Autowired
	BTaskService bTaskService;

	/**
	 * 新增任务接口
	 * 
	 * @param BTask
	 * @return
	 */
	@RequestMapping(method = { RequestMethod.POST }, value = "")
	@ResponseBody
	public ResponseResult add(@RequestBody BTask bTask, HttpServletRequest request) {
		BEmployee user = (BEmployee) request.getAttribute("user");
		Integer flag = 0;
		long cycleTaskEnd = 66600000;
		if (user != null && bTask.getName() != null && bTask.getLastEndTime() != null && bTask.getDescription() != null
				&& bTask.getExecEmpId() != null && bTask.getType() != null) {
			Date date = new Date();
			bTask.setCreateTime(date);
			bTask.setCreateEmpId(user.getId());
			bTask.setId(Common.generateUUID());
			bTask.setComState("0");
			bTask.setMsgState("0");
			if(bTask.getCycleTime().equals("DAY")){
				bTask.setCycleTaskEnd(cycleTaskEnd);
			}
			flag = bTaskService.insert(bTask);
			// 判断循环任务

			// 获取当前周一0点的时间
			Calendar date1 = Calendar.getInstance(Locale.CHINA);
			date1.setFirstDayOfWeek(Calendar.MONDAY);
			date1.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
			date1.set(Calendar.HOUR_OF_DAY, 0);
			date1.set(Calendar.MINUTE, 0);
			date1.set(Calendar.SECOND, 0);
			date1.set(Calendar.MILLISECOND, 0);
			long weekTimes = new Date().getTime() - date1.getTime().getTime();
			// 获取本月一号0点的时间
			Calendar date2 = Calendar.getInstance(Locale.CHINA);
			date2.set(Calendar.DAY_OF_MONTH, 1);
			date2.set(Calendar.HOUR_OF_DAY, 0);
			date2.set(Calendar.MINUTE, 0);
			date2.set(Calendar.SECOND, 0);
			date2.set(Calendar.MILLISECOND, 0);
			// 当前时间与月1号时间差
			long monthTime = new Date().getTime() - date1.getTime().getTime();
			// 获取当天零点的时间
			Calendar date3 = Calendar.getInstance(Locale.CHINA);
			date3.set(Calendar.HOUR_OF_DAY, 0);
			date3.set(Calendar.MINUTE, 0);
			date3.set(Calendar.SECOND, 0);
			date3.set(Calendar.MILLISECOND, 0);
			long dayTimes = new Date().getTime() - date3.getTime().getTime();
			
            if (bTask.getCycleTime().equals("DAY") && cycleTaskEnd > dayTimes) {
				bTask.setCycleTime("NONE");
				bTask.setLastEndTime(new Date(date3.getTime().getTime() + bTask.getCycleTaskEnd()));// 为了捕获到周期项目的截止时间
				bTask.setCycleTaskEnd(null);
				bTask.setParentTaskId(bTask.getId());
				bTask.setId(Common.generateUUID());
				bTaskService.insert(bTask);
			} else if (bTask.getCycleTime().equals("WEEK") && bTask.getCycleTaskEnd() > weekTimes) {
				bTask.setCycleTime("NONE");
				bTask.setCycleTaskEnd(null);
				bTask.setLastEndTime(new Date(date1.getTime().getTime() + bTask.getCycleTaskEnd()));
				bTask.setParentTaskId(bTask.getId());
				bTask.setId(Common.generateUUID());
				bTaskService.insert(bTask);
			} else {

				if (bTask.getCycleTime().equals("MONTH") && bTask.getCycleTaskEnd() > monthTime && date1.get(Calendar.MONTH) + 1 != 2) {
					bTask.setLastEndTime(new Date(date2.getTime().getTime() + bTask.getCycleTaskEnd()));
					bTask.setParentTaskId(bTask.getId());
					bTask.setId(Common.generateUUID());
					bTask.setCycleTime("NONE");
					bTaskService.insert(bTask);
				} else if (bTask.getCycleTime().equals("MONTH") && bTask.getCycleTaskEnd() > monthTime
						&& date1.get(Calendar.MONTH) + 1 == 2) {
					bTask.setLastEndTime(new Date(date2.getTime().getTime() + bTask.getCycleTaskEnd()));
					bTask.setCycleTime("NONE");
					bTask.setParentTaskId(bTask.getId());
					bTask.setId(Common.generateUUID());
					if (bTask.getCycleTaskEnd().longValue() >= (long) (2592000 * 1000)) {
						bTask.setCycleTaskEnd((long) (2419200 * 1000));
						bTaskService.insert(bTask);
					}
                }
			}
			if (flag <= 0) {
				return ResponseResult.fail("插入失败", null);
			}
			return ResponseResult.success("添加资源成功", bTask);
		}
		return ResponseResult.fail("用户为空或新建任务参数为空", null);
	}

	/**
	 * 修改任务状态接口
	 * 
	 * @param id
	 * @return
	 * @see 暂无此操作
	 */
	@RequestMapping(method = { RequestMethod.PATCH }, value = "/{id}")
	@ResponseBody
	public ResponseResult update(@PathVariable String id, @RequestBody Map<String, Object> map,
			HttpServletRequest request) {
		BEmployee user = (BEmployee) request.getAttribute("user");
		Integer flag = 0;
		BTask bTask = new BTask();
		bTask.setId(id);
		bTask.setComState((String) map.get("comState"));
		bTask.setComEndTime(new Date());
		// System.out.println(bTask.getComEndTime());
		if (map.get("createId") != user.getId() && !user.getRole().equals("ADMIN")) {
			if (id != null && bTask != null) {
				BTask bt = bTaskService.findById(id);

				if (!bt.getExecEmpId().equals(user.getId()) && !bt.getCreateEmpId().equals(user.getId())) {
					return ResponseResult.fail("这不是你的任务", false);
				}
			}

			flag = bTaskService.updateComState(id, bTask);
			if (flag > 0) {
				BTask bt = bTaskService.findById(id);
				return ResponseResult.success("修改资源成功", bt);
			} else {
				return ResponseResult.fail("修改资源失败", flag);
			}
		} else {
			flag = bTaskService.updateComState(id, bTask);
			if (flag > 0) {
				BTask bt = bTaskService.findById(id);
				
				return ResponseResult.success("修改资源成功", bt);
			} else {
				return ResponseResult.fail("修改资源失败", flag);
			}
		}
	}
	
	/**
	 * 任务移除
	 */
	
	@RequestMapping(method = { RequestMethod.DELETE }, value = "/{id}")
	@ResponseBody
	public ResponseResult deleteById(@PathVariable String id){
		
		int flag = bTaskService.deleteById(id);
		if(flag==1){
			return ResponseResult.success("删除任务成功", flag);
		}
		return ResponseResult.fail("删除任务失败", flag);
	}
	
	
}
